//
//  XCFeedBackViewController.m
//  Project
//
//  Created by 苟亚明 on 16/7/1.
//
//

#import "XCFeedBackViewController.h"

@interface XCFeedBackViewController ()
@property (weak, nonatomic) IBOutlet UILabel *lblNumber;
@property (weak, nonatomic) IBOutlet UITextView *txtView;
@property (weak, nonatomic) IBOutlet UILabel *lblPlacehouder;
@property (nonatomic, weak) UIButton *complexBtn;

@end

@implementation XCFeedBackViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    UIImage *image = [UIImage imageNamed:@"transparent-1"];
    [self.navigationController.navigationBar setBackgroundImage:image
                                                  forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:image];
    
}
- (void)leftBtnClick{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.title = @"反馈";
    
    self.txtView.layer.cornerRadius = 7.0f;
    
    UIButton *complexBtn = [[UIButton alloc]init];
    
    complexBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -30);
    
    complexBtn.frame = CGRectMake(0, 0, 60, 40);
    
    [complexBtn setTitle:@"完成" forState:UIControlStateNormal];
    
    [complexBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
    

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChangged:) name:UITextViewTextDidChangeNotification object:nil];
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"arrow_back" highImage:@"arrow_back" target:self action:@selector(leftBtnClick)];
}



- (void)textChangged:(NSNotification *)note{
    
    UITextView *txtView = note.object;
    NSString *text = txtView.text;

    NSString *lang = self.textInputMode.primaryLanguage;
    
    if ([lang isEqualToString:@"zh-Hans"]) {
        
        NSLog(@"primaryLanguage");
        
        UITextRange *selectedRange = [txtView markedTextRange];
        
        if (!selectedRange) {
            if(text.length > 0){
                
/****完成按钮注册在这里写******/
                self.lblPlacehouder.hidden = YES;
                self.lblNumber.text = [NSString stringWithFormat:@"%zd/200",text.length];
            }else{
                self.lblNumber.text = @"0/200";
                self.lblPlacehouder.hidden = NO;
            }
            if (text.length > 200) {
                txtView.text = [text substringToIndex:200];
            }
        }
        
        
    }else{
        if(text.length > 0){
/***** 完成按钮注册在这里写******/
            self.lblPlacehouder.hidden = YES;
            self.lblNumber.text = [NSString stringWithFormat:@"%zd/200",text.length];
        }else{
            self.lblNumber.text = @"0/200";
            self.lblPlacehouder.hidden = NO;
        }
        if (text.length > 200) {
            txtView.text = [text substringToIndex:200];
        }
        
    }

    
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
